<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>所有人文章列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">

    <script src="js/jquery.min.js"></script>
    <style>
        .nav {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }

        .container {
            padding-top: 80px;
            height: auto;
        }

        .container-right {
            width: auto;
        }

        .blog-pagnation-wrapper {
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }

        .blog-pagnation-item {
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }

        .blog-pagnation-item:hover {
            background: #4e4eeb;
            color: #fff;
        }

        .blog-pagnation-item.actvie {
            background: #4e4eeb;
            color: #fff;
        }

        .search-container {
            width: 300px;
            margin: 0 auto;
            padding: 10px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .search-container input[type="text"] {
            flex: 1;
            padding: 8px;
            font-size: 16px;
        }

        .search-container input[type="submit"] {
            padding: 8px;
            font-size: 16px;
            margin-left: 10px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpg" alt="">
        <span class="title" style="font-size:30px">优文共享社区</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="login.html">登陆</a>
        <a href="myblog_list.html">个人主页</a>
        <a href="blog_list.html">文章展示页</a>
        <a href="blog_add.html">写文章</a>
        <a href="javascript:logout()">注销</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- <div class="container-left">

        </div> -->
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width:100%;">
            <div class="search-container">
                <input type="text" id="search-input" placeholder="请输入文章标题搜索">
                <input type="submit" value="搜索" onclick="qrylist()">
                <input type="submit" value="重置" onclick="initPage()">
            </div>
            <div id="artListDiv">
            </div>
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="goFirstPage()">首页</button>
                <button class="blog-pagnation-item" onclick="goBeforePage()">上一页</button>
                <div class="blog-pagnation-item" id="nowpindex"></div>
                <!-- <div class="blog-pagnation-item" id="newinputpsize"></div>  -->
                <button class="blog-pagnation-item" onclick="goNextPage()">下一页</button>
                <button class="blog-pagnation-item" onclick="goLastPage()">末页</button>
            </div>
        </div>
    </div>

    <script>


        //获取url中的内容
        function getUrlValue(key) {
            //获取url中的内容
            var params = location.search;
            if (params.length > 1) {
                params = location.search.substring(1);
                var paramArr = params.split("&");
                for (var i = 0; i < paramArr.length; i++) {
                    var kv = paramArr[i].split("=");
                    if (kv[0] == key) {
                        return kv[1];
                    }
                }
            }
            return "";
        }

        //当前页码
        var pindex = 1;
        //每页显示条数
        var psize = 3;
        //最大页数
        var pcount = 1;
        //1、先获取当前页面url中的页码
        pindex = getUrlValue("pindex") == "" ? 1 : getUrlValue("pindex");
        psize = getUrlValue("psize") == "" ? 1 : getUrlValue("psize");


        //2、查询后端接口获取当前页面数据进行展示
        function initPage() {
            jQuery.ajax({
                url: "/art/listbypage",
                type: "POST",
                data: { "pindex": pindex, "psize": psize },
                success: function (result) {
                    if (result != null && result.code == 200 && result.data.list.length > 0) {
                        var atricleHtml = "";
                        for (var i = 0; i < result.data.list.length; i++) {
                            var articleinfo = result.data.list[i];
                            var userinfo = result.data.userinfolist[i];
                            atricleHtml += '<div class="blog">';
                            atricleHtml += '<div class="title">' + articleinfo.title + '</div>';
                            atricleHtml += '<div class="articleusername">' + '作者：' + userinfo.username + '</div>';
                            atricleHtml += '<div class="date">' + articleinfo.updatetime + '</div>';
                            atricleHtml += '<div class="desc">' + articleinfo.content + '</div>';
                            atricleHtml += '<a href="blog_content.html?id=' + articleinfo.id + '" class="detail">查看全文 &gt;&gt;</a>';
                            atricleHtml += '</div>';
                        }
                        jQuery("#artListDiv").html(atricleHtml);
                        pcount = result.data.pcount;
                        jQuery("#nowpindex").html(pindex);
                    }
                }
            });
        }
        initPage();

        // 根据文章标题或者作者搜索
        function qrylist() {
            var search = jQuery("#search-input");
            if (search.val() == "") {
                alert("请先输入查询的内容！");
                search.focus();
                return;
            }
            jQuery.ajax({
                url: "/art/qrylist",
                type: "POST",
                data: { "pindex": pindex, "psize": psize, "title": search.val() },
                success: function (result) {
                    if (result.code == -1 && result.msg == "你搜索的内容为空!") {
                        alert("您搜索的内容为空！");
                        return;
                    }
                    else if (result != null && result.code == 200 && result.data.list.length != undefined && result.data.list.length > 0) {
                        var atricleHtml = "";
                        for (var i = 0; i < result.data.list.length; i++) {
                            var articleinfo = result.data.list[i];
                            var userinfo = result.data.userinfolist[i];
                            atricleHtml += '<div class="blog">';
                            atricleHtml += '<div class="title">' + articleinfo.title + '</div>';
                            atricleHtml += '<div class="articleusername">' + '作者：' + userinfo.username + '</div>';
                            atricleHtml += '<div class="date">' + articleinfo.updatetime + '</div>';
                            atricleHtml += '<div class="desc">' + articleinfo.content + '</div>';
                            atricleHtml += '<a href="blog_content.html?id=' + articleinfo.id + '" class="detail">查看全文 &gt;&gt;</a>';
                            atricleHtml += '</div>';
                        }
                        jQuery("#artListDiv").html(atricleHtml);
                        pcount = result.data.pcount;
                        jQuery("#nowpindex").html(pindex);
                    }
                }
            });
        }

        //首页
        function goFirstPage() {
            if (pindex <= 1) {
                alert("当前页面是首页了！");
                return;
            }
            location.href = "blog_list.html";
        }

        //上一页
        function goBeforePage() {
            if (pindex <= 1) {
                alert("当前页面是首页了！");
                return;
            }
            pindex = parseInt(pindex) - 1;
            location.href = "blog_list.html?pindex=" + pindex;
        }

        //下一页
        function goNextPage() {
            if (pindex >= pcount) {
                alert("您已经在末页了！");
                return;
            }
            pindex = parseInt(pindex) + 1;
            location.href = "blog_list.html?pindex=" + pindex;
        }

        //末页
        function goLastPage() {
            if (pindex >= pcount) {
                alert("您已经在末页了！");
                return;
            }
            location.href = "blog_list.html?pindex=" + pcount;
        }

        //注销操作（退出登陆状态）
        function logout() {
            if (confirm("确认退出登录？")) {
                jQuery.ajax({
                    url: "/user/logout",
                    type: "POST",
                    data: {},
                    success: function (result) {
                        if (result != null && result.code == 200) {
                            location.href = "/login.html";
                        } else {
                            alert("您是游客模式，请先返回登陆！");
                        }
                    }
                });
            }
        }
    </script>
</body>

</html>